using System;
using System.Windows.Forms;

using Rsdn.Janus.ObjectModel;
using Rsdn.TreeGrid;
using Rsdn.SmartApp;

namespace Rsdn.Janus
{
	/// <summary>
	/// Summary description for Outbox.
	/// </summary>
	public class OutboxFeature : Feature, IGetData
	{
		private static OutboxFeature _instance;

		public static OutboxFeature Instance
		{
			get { return _instance; }
		}

		private readonly IServiceProvider _provider;

        public OutboxFeature(IServiceProvider provider)
		{
			_instance = this;
			_description = SR.Forum.Outcoming.DisplayName;
            _provider = provider;

            IIncomingFolderUpdate folderUpdate = (IIncomingFolderUpdate)provider.GetService(typeof(IIncomingFolderUpdate));
            folderUpdate.AddUpdate(this);
		}

		public override string Key
		{
			get { return "Outbox"; }
		}

	    public override bool IsUpdateNeed(object msg)
	    {
            // don't need to update this future folder if any message changed. 
            return false;
        }

	    public override void Refresh()
	    {
            IOutboxManager outboxManager = _provider.GetRequiredService<IOutboxManager>();
            //outboxManager.RefreshMessages();
	    }

	    public override int ImageIndex
		{
			get { return Features.Instance.OutboxImageIndex; }
		}

		#region IFeatureGui

		protected override Control CreateGuiControl()
		{
			return
				(Control)_provider
					.GetRequiredService<IOutboxManager>()
					.OutboxForm;
		}

		#endregion IFeatureGui

		#region IGetData
		void IGetData.GetData(NodeInfo nodeInfo, CellInfo[] cellData)
		{
			var om = _provider.GetRequiredService<IOutboxManager>();
			cellData[0].Text = Description;
			cellData[0].ImageIndex = ImageIndex;
			cellData[1].Text = string.Format("{0}/{1}/{2}",
				om.NewMessages.Count    == 0 ? string.Empty : om.NewMessages.Count.ToString(),
				om.RateMarks.Count      == 0 ? string.Empty : om.RateMarks.Count.ToString(),
				om.DownloadTopics.Count == 0 ? string.Empty : om.DownloadTopics.Count.ToString());

			nodeInfo.Highlight = (om.NewMessages.Count > 0)
				|| (om.RateMarks.Count > 0) || (om.DownloadTopics.Count > 0);
		}
		#endregion IGetData
	}
}